package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.IotEnterpriseManageRpcService;
import com.hunttown.mes.rpc.domain.IotEnterpriseManageDTO;
import com.hunttown.mes.rpc.domain.query.IotEnterpriseManageDTOQuery;
import com.hunttown.mes.service.IotEnterpriseManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.IotEnterpriseManageConverts.convert;

/**
 * created by wangjunfu on 2021-02-20 11:33:48
 */
@Service
public class IotEnterpriseManageRpcServiceImpl implements IotEnterpriseManageRpcService {

    private final Logger logger = LoggerFactory.getLogger(IotEnterpriseManageRpcServiceImpl.class);

    private final IotEnterpriseManageService iotEnterpriseManageService;

    @Autowired
    public IotEnterpriseManageRpcServiceImpl(IotEnterpriseManageService iotEnterpriseManageService) {
        this.iotEnterpriseManageService = iotEnterpriseManageService;
    }

    @Override
    public IotEnterpriseManageDTO insert(IotEnterpriseManageDTO iotEnterpriseManageDTO) {
        try {
            return convert(iotEnterpriseManageService.insert(convert(iotEnterpriseManageDTO)));
        } catch (Exception e) {
            logger.error("IotEnterpriseManage create failed.", e);
            throw new RuntimeException("IotEnterpriseManage create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(IotEnterpriseManageDTO iotEnterpriseManageDTO) {
        try {
            return iotEnterpriseManageService.updateInfoById(convert(iotEnterpriseManageDTO));
        } catch (Exception e) {
            logger.error("IotEnterpriseManage update by id failed.", e);
            throw new RuntimeException("IotEnterpriseManage update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(IotEnterpriseManageDTOQuery iotEnterpriseManageDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (iotEnterpriseManageDTOQuery.getW_id() != null && iotEnterpriseManageDTOQuery.getW_id() > 0) {
                query.put("w_id", iotEnterpriseManageDTOQuery.getW_id());
            }

            if (iotEnterpriseManageDTOQuery.getW_manageId() != null) {
                query.put("w_manageId", iotEnterpriseManageDTOQuery.getW_manageId());
            }

            if (iotEnterpriseManageDTOQuery.getW_entId() != null) {
                query.put("w_entId", iotEnterpriseManageDTOQuery.getW_entId());
            }

            if (iotEnterpriseManageDTOQuery.getIds() != null && !iotEnterpriseManageDTOQuery.getIds().equals("")) {
                query.put("ids", iotEnterpriseManageDTOQuery.getIds());
            }

            if (iotEnterpriseManageDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", iotEnterpriseManageDTOQuery.getStopFlag());
            }

            if (iotEnterpriseManageDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", iotEnterpriseManageDTOQuery.getDeleteFlag());
            }

            return iotEnterpriseManageService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("IotEnterpriseManage update by query failed.", e);
            throw new RuntimeException("IotEnterpriseManage update by query failed, Please order your code.", e);
        }
    }

    @Override
    public IotEnterpriseManageDTO selectById(Integer id) {
        try {
            return convert(iotEnterpriseManageService.selectById(id));
        } catch (Exception e) {
            logger.error("IotEnterpriseManage select by id failed.", e);
            throw new RuntimeException("IotEnterpriseManage select by id failed, Please order your code.", e);
        }
    }

    @Override
    public IotEnterpriseManageDTO selectObjByQuery(IotEnterpriseManageDTOQuery iotEnterpriseManageDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (iotEnterpriseManageDTOQuery.getManageId() != null && iotEnterpriseManageDTOQuery.getManageId() > 0) {
                query.put("manageId", iotEnterpriseManageDTOQuery.getManageId());
            }

            if (iotEnterpriseManageDTOQuery.getEntId() != null && iotEnterpriseManageDTOQuery.getEntId() > 0) {
                query.put("entId", iotEnterpriseManageDTOQuery.getEntId());
            }

            if (StringUtils.isNotBlank(iotEnterpriseManageDTOQuery.getAdminName())) {
                query.put("adminName", iotEnterpriseManageDTOQuery.getAdminName());
            }

            if (iotEnterpriseManageDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", iotEnterpriseManageDTOQuery.getStopFlag());
            }

            if (iotEnterpriseManageDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", iotEnterpriseManageDTOQuery.getDeleteFlag());
            }

            return convert(iotEnterpriseManageService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("IotEnterpriseManage select obj by query failed.", e);
            throw new RuntimeException("IotEnterpriseManage select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<IotEnterpriseManageDTO> getListForPage(IotEnterpriseManageDTOQuery iotEnterpriseManageDTOQuery) {
        Query query = Query.page(iotEnterpriseManageDTOQuery.getPn(), iotEnterpriseManageDTOQuery.getPageSize());

        if (iotEnterpriseManageDTOQuery.getId() != null && iotEnterpriseManageDTOQuery.getId() > 0) {
            query.put("id", iotEnterpriseManageDTOQuery.getId());
        }

        if (iotEnterpriseManageDTOQuery.getIds() != null && !iotEnterpriseManageDTOQuery.getIds().equals("")) {
            query.put("ids", iotEnterpriseManageDTOQuery.getIds());
        }

        if (iotEnterpriseManageDTOQuery.getManageId() != null && iotEnterpriseManageDTOQuery.getManageId() > 0) {
            query.put("manageId", iotEnterpriseManageDTOQuery.getManageId());
        }

        if (StringUtils.isNotBlank(iotEnterpriseManageDTOQuery.getAdminName())) {
            query.put("adminName", iotEnterpriseManageDTOQuery.getAdminName());
        }

        if (StringUtils.isNotBlank(iotEnterpriseManageDTOQuery.getLikeName())) {
            query.put("likeName", iotEnterpriseManageDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(iotEnterpriseManageDTOQuery.getShowDate())) {
            query.put("showDate", iotEnterpriseManageDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(iotEnterpriseManageDTOQuery.getStartDate())) {
            query.put("startDate", iotEnterpriseManageDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(iotEnterpriseManageDTOQuery.getEndDate())) {
            query.put("endDate", iotEnterpriseManageDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(iotEnterpriseManageDTOQuery.getOrderBy())) {
            query.put("orderBy", iotEnterpriseManageDTOQuery.getOrderBy());
        }

        if (iotEnterpriseManageDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", iotEnterpriseManageDTOQuery.getStopFlag());
        }

        if (iotEnterpriseManageDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", iotEnterpriseManageDTOQuery.getDeleteFlag());
        }

        List<IotEnterpriseManageDTO> list = convert(iotEnterpriseManageService.selectByQuery(query));
        Page<IotEnterpriseManageDTO> page = Page.from(iotEnterpriseManageDTOQuery.getPn(), iotEnterpriseManageDTOQuery.getPageSize(), list);
        page.setTotalItems(iotEnterpriseManageService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return iotEnterpriseManageService.deleteById(id);
        } catch (Exception e) {
            logger.error("IotEnterpriseManage delete failed.", e);
            throw new RuntimeException("IotEnterpriseManage delete failed, Please order your code.", e);
        }
    }
}